package com.zikao.article.service.impl;

import com.zikao.article.entity.TextEntity;
import com.zikao.article.service.TextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.Query;

import com.zikao.article.dao.RemarkDao;
import com.zikao.article.entity.RemarkEntity;
import com.zikao.article.service.RemarkService;


@Service("remarkService")
public class RemarkServiceImpl extends ServiceImpl<RemarkDao, RemarkEntity> implements RemarkService {
    @Autowired
    private TextService textService;

    @Override
    public List<RemarkEntity> selectByParams(Map<String, Object> params) {
        List<RemarkEntity> remarkList = baseMapper.selectByMap(params);
        List<Long> textIds = remarkList.stream().map((r) -> {
            return r.getTextId();
        }).collect(Collectors.toList());

        if (textIds.size()>0){
            List<TextEntity> textList = textService.selectBatchIds(textIds);
            Map<Long, TextEntity> textMap = textList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));

            for (RemarkEntity remark : remarkList){
                TextEntity text = textMap.get(remark.getTextId());
                remark.setContent(text.getContent());
            }
        }
        return remarkList;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RemarkEntity> page = this.page(
                new Query<RemarkEntity>().getPage(params),
                new QueryWrapper<RemarkEntity>()
        );

        return new PageUtils(page);
    }

}